<?php

use App\Models\Repository;
use Illuminate\Database\Seeder;
use Illuminate\Support\Carbon;

class RsahzMigrationSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $fictions = \Illuminate\Support\Facades\DB::connection('rsahz')->table('fiction')->get();
        foreach ($fictions as $fiction) {
            $repository = new Repository(['title' => '[柔术爱好者文章迁移] ' . $fiction->title, 'url' => $fiction->url ?? null, 'type' => $fiction->visible ? 0 : 3]);
            $repository->timestamps = false;
            $ts = $fiction->update ? new Carbon($fiction->update) : null;
            $repository->created_at = $ts;
            $repository->updated_at = $ts;
            $repository->save();


            $split_contents = preg_split('/\s#{1,6} /', $fiction->content);

            if (count($split_contents) > 1) {
                foreach ($split_contents as $index=> $split_content) {
                    $chap=[];
                    $exp = explode("\n", $split_content);
                    if (mb_strlen($exp[0])<=50){
                        $chap['title'] = ltrim($exp[0],'# ') ;
                        array_shift($exp);
                    }


                    $chap['content'] = implode("\n", $exp);
                    $chapter = new \App\Models\Chapter($chap);
                    $chapter->timestamps = false;
                    $chapter->created_at = $ts;
                    $chapter->updated_at = $ts;
                    $repository->increment('length',mb_strlen($chapter->content));
                    if (has_adult_content($chapter->content)) {
                        $chapter->status = $chapter::STATUS_ADULT;
                    }
                    $chapter->save();
                    $repository->chapters()->attach($chapter, ['sequence' => $index+1, 'is_using' => 1]);
                }
            } else {
                $chap=[];
                $chap['content'] = $split_contents[0];
                $chapter = new \App\Models\Chapter($chap);
                $chapter->timestamps = false;
                $chapter->created_at = $ts;
                $chapter->updated_at = $ts;
                if (has_adult_content($chapter->content)) {
                    $chapter->status = $chapter::STATUS_ADULT;
                }
                $repository->increment('length',mb_strlen($chapter->content));
                $chapter->save();
                $repository->chapters()->attach($chapter, ['sequence' => 1, 'is_using' => 1]);
            }





        }

        $zlata_stories = \Illuminate\Support\Facades\DB::connection('rsahz')->table('zlata')->get();

        foreach ($zlata_stories as $zlata_story) {
            $repository = new Repository(['title' => '[zlata.de文章迁移] ' . $zlata_story->title_en . ' / ' . $zlata_story->title_de, 'url' => 'http://zlata.de/stories.php?story=' . $zlata_story->id]);
            $repository->timestamps = false;
            $repository->save();

            $chapter = new \App\Models\Chapter(['title' => $zlata_story->title_en, 'content' => $zlata_story->content_en]);
            $chapter->timestamps = false;
            $repository->increment('length',mb_strlen($chapter->content));
            $chapter->save();
            $repository->chapters()->attach($chapter, ['sequence' => 1, 'is_using' => 1]);
            $chapter = new \App\Models\Chapter(['title' => $zlata_story->title_de, 'content' => $zlata_story->content_de]);
            $chapter->timestamps = false;
            $repository->increment('length',mb_strlen($chapter->content));
            $chapter->save();
            $repository->chapters()->attach($chapter, ['sequence' => 2, 'is_using' => 1]);
        }
    }
}
